package cn.stylefeng.guns.modular.controller;


import java.util.Date;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.IdjInformation;
import cn.stylefeng.guns.modular.service.IIdjInformationService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;

/**
 * <p>
 * 资讯表 前端控制器
 * </p>
 *
 * @author TANGWJ
 * @since 2020-07-07
 */
@Controller
@RequestMapping("/modular/information")
public class IdjInformationController {

	@Autowired
	private IIdjInformationService service;
	
	private static  final int SHORT_MAX=65536;
	 
	private static int counter=-1;

	private static String PREFIX = "/information/";

	/**
	 * 跳转首页
	 */
	@RequestMapping
	public String index() {
		return PREFIX + "information.html";
	}
	
	@RequestMapping("/add")
	public String add() {
		return PREFIX + "information_add.html";
	}
	
	@RequestMapping("/addfiles")
	public String addfiles() {
		return PREFIX + "information_addfile.html";
	}
	
	 /**
     * 获取列表
     */
    @ResponseBody
    @RequestMapping("list")
    public LayuiPageInfo list(String keyword, Integer userType, String timeLimit, String timeLimits, Integer status, Integer sex) {
        //获取分页参数
        Page<IdjInformation> page = LayuiPageFactory.defaultPage();
        //拼接查询条件
        String beginTime = null;
        String endTime = null;
        if (ToolUtil.isNotEmpty(timeLimit)) {
            String[] split = timeLimit.split(" - ");
            beginTime = split[0];
            endTime = split[1];
        }
       	String order="order";
    	String no="no";
        //根据条件查询日志
        return service.getList(page, keyword, beginTime, endTime, null, no, status, null, order);
    }
    
    /**
     * 获取列表
     */
    @ResponseBody
    @RequestMapping("lists")
    public LayuiPageInfo lists(String keyword,Integer userType, String timeLimit, Integer status) {
    	//获取分页参数
    	Page<IdjInformation> page = LayuiPageFactory.defaultPage();
    	//拼接查询条件
    	String beginTime = null;
    	String endTime = null;
    	if (ToolUtil.isNotEmpty(timeLimit)) {
    		String[] split = timeLimit.split(" - ");
    		beginTime = split[0];
    		endTime = split[1];
    	}
    	String yes="yes";
        String orderBy="orderBy";
    	//根据条件查询日志
    	return service.getList(page, keyword, beginTime, endTime, yes, null, status, orderBy, null);
    }
    
    @RequestMapping("details/{informationId}")
    public ModelAndView add(@PathVariable Long informationId) {
        ModelAndView index = service.edit(informationId);
        index.setViewName(PREFIX + "information_details.html");
        return index;
    }
    
	@RequestMapping("/update")
	@ResponseBody
	public ResponseData update(@RequestBody IdjInformation information) {
		if (ToolUtil.isEmpty(information.getInformationId())) {
			throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
		}
		if (information.getStatus() == 1) {
			information.setReleaseTime(new Date());
		}
		service.setStatus(information);
		return ResponseData.success();
	}
	
	@RequestMapping("/insert")
	@ResponseBody
	public ResponseData insert(@RequestBody IdjInformation information) {
		information.setInformationId(nextId());
		information.setUserId(LoginContextHolder.getContext().getUserId());
		information.setCreateTime(new Date());
		if (information.getStatus() == 1) {
			information.setReleaseTime(new Date());
		}
		service.insert(information);
		return ResponseData.success();
	}
	
	@RequestMapping("/delete/{informationId}")
	@ResponseBody
	public ResponseData delete(@PathVariable Long informationId) {
		if (ToolUtil.isEmpty(informationId)) {
			throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
		}
		service.delete(informationId);
		return ResponseData.success();
	}
	
	public static synchronized long nextId() {
        long now = System.currentTimeMillis();
        if (counter == -1) {
            long seed = now ^ Thread.currentThread().getId();
            Random rnd = new Random(Long.hashCode(seed));
            counter = rnd.nextInt(SHORT_MAX);
        }
        long id = (now << 16) | counter;
        counter = (counter + 1) % SHORT_MAX;
        return id;
    }
	
}

